package com.yidi.system.service.imp;

import com.yidi.common.core.page.TableDataInfo;
import com.yidi.common.core.service.imp.AbstractBaseService;
import com.yidi.common.core.text.Convert;
import com.yidi.common.enums.RemoveEnum;
import com.yidi.common.utils.StringUtils;

import com.yidi.system.domain.SysLogininfor;
import com.yidi.system.repository.SysLogininforRepository;
import com.yidi.system.service.SysLogininforService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Predicate;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：Xn
 * @ClassName: SysLogininforServiceImpl
 * @date ：2021-10-19 14:15
 * @describe ：
 */
@Service
public class SysLogininforServiceImpl extends AbstractBaseService<SysLogininfor> implements SysLogininforService {

    @Autowired
    private SysLogininforRepository sysLogininforRepository;

    /**
     * 分页
     */
    @Override
    public Page<SysLogininfor> findByPage(Integer pageNo, Integer pageSize, SysLogininfor logininfor) {
        List<Sort.Order> orders = new ArrayList<>();
        orders.add(new Sort.Order(Sort.Direction.DESC, "id"));
        Sort sort = Sort.by(orders);
        Pageable pageable = PageRequest.of(pageNo, pageSize, sort);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        Page<SysLogininfor> page = sysLogininforRepository.findAll((root, query, cb) ->{
            List<Predicate> list = new ArrayList<>();
            list.add(cb.and(cb.equal(root.get("removeStatus").as(Integer.class), RemoveEnum.NOT_REMOVE.getCode())));

            if(StringUtils.isNotBlank(logininfor.getIpaddr())) {
                list.add(cb.and(cb.like(root.get("ipaddr").as(String.class), logininfor.getIpaddr())));
            }

            if(StringUtils.isNotBlank(logininfor.getStatus())) {
                list.add(cb.and(cb.equal(root.get("status").as(String.class), logininfor.getStatus())));
            }

            if(StringUtils.isNotBlank(logininfor.getUserName())) {
                list.add(cb.and(cb.like(root.get("userName").as(String.class), logininfor.getUserName())));
            }

            Map<String, Object> params = logininfor.getParams();
            if(params != null) {
                String beginTime = (String) params.get("beginTime");
                String endTime = (String) params.get("endTime");
                if(StringUtils.isNotBlank(beginTime)) {
                    try {
                        list.add(cb.and(cb.greaterThanOrEqualTo(root.get("loginTime"), sdf.parse(beginTime))));
                    } catch (ParseException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                if(StringUtils.isNotBlank(endTime)) {
                    try {
                        list.add(cb.and(cb.lessThanOrEqualTo(root.get("loginTime"), sdf.parse(endTime))));
                    } catch (ParseException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }

            Predicate[] p = new Predicate[list.size()];
            return cb.and(list.toArray(p));
        },pageable);

        return page;
    }

    /**
     * 新增系统登录日志
     *
     * @param logininfor 访问日志对象
     */
    @Override
    public void insertLogininfor(SysLogininfor logininfor)
    {
        sysLogininforRepository.save(logininfor);
    }

    /**
     * 批量删除系统登录日志
     *
     * @param ids 需要删除的数据
     * @return
     */
    @Override
    public void deleteLogininforByIds(Long[] ids)
    {
        for(Long id : ids) {
            deleteLogininforById(id);
        }
    }

    /**
     * 删除任务日志
     * @param logininforId 调度日志ID
     */
    @Override
    public void deleteLogininforById(Long logininforId)
    {
        SysLogininfor logininfor = findById(logininforId);
        if(logininfor != null) {
            delete(logininforId);
        }
    }

    /**
     * 清空系统登录日志
     */
    @Override
    public void cleanLogininfor()
    {
        deleteAll();
    }
}
